﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Configuration;
using System.Reflection;


namespace Ict.DataAccess
{
    /// <summary>
    /// 用于读取数据库设置，并根据数据库设置创建用于查询和命令操作的具体类
    /// </summary>
    public abstract class DataContext
    {
        /// <summary>
        /// 装入配置
        /// </summary>
        /// <returns></returns>
        private static Configuration.DataConfigSection LoadConfigSession()
        {
            return ((ConfigurationManager.GetSection("DataAccessSession") as Configuration.DataConfigSection) == null) ?
                new Configuration.DataConfigSection() : (ConfigurationManager.GetSection("DataAccessSession") as Configuration.DataConfigSection);
        }

        private static readonly Configuration.DataConfigSection configSection = LoadConfigSession();


        static private Assembly _asm = Assembly.Load(configSection.DataAccessAssembly.AssemblyName);
        static private ConnectionStringSettings _connSettings = ConfigurationManager.ConnectionStrings[configSection.ConnectionName];

        private static DataContext current = null;

        public static DataContext Current
        {
            get
            {
                if (current == null)
                {
                    
                    current = (DataContext)_asm.CreateInstance(
                        configSection.DataAccessAssembly.DataContextClass, true,
                        BindingFlags.CreateInstance, null,
                        null, null, null);
                }
                return current;
            }
        }

        /// <summary>
        /// 根据数据库连接串的Provider，创建查询器
        /// </summary>
        /// <returns>查询器实例</returns>
        public static Executor CreateExecutor()
        {
            return (Executor)Current.CreateExecutor(_connSettings.ConnectionString);
        }

        /// <summary>
        /// 根据数据库连接串的Provider，创建数据操作语句构造器
        /// </summary>
        /// <returns>数据操作语句构造器实例</returns>
        public static DmlBuilder CreateDmlBuilder()
        {
            return (DmlBuilder)_asm.CreateInstance(configSection.DataAccessAssembly.DmlBuilderClass, true,
                BindingFlags.CreateInstance, null,
                null, null, null);
        }

        public static Linq.Selectable<TEntity> Select<TEntity>()
        {
            return new Linq.Selectable<TEntity>();
        }

        public static Linq.Insertable<TEntity> Insert<TEntity>()
        {
            return new Linq.Insertable<TEntity>();
        }

        public static Linq.Updatable<TEntity> Update<TEntity>()
        {
            return new Linq.Updatable<TEntity>();
        }

        public static Linq.Deletable<TEntity> Delete<TEntity>()
        {
            return new Linq.Deletable<TEntity>();
        }

        #region 可供扩展的虚函数

        /// <summary>
        /// 创建能够提供SQL编码的编码器
        /// </summary>
        /// <returns>编码器实例</returns>
        public abstract DmlEncoder CreateSqlEncoder();

        /// <summary>
        /// 创建用于执行SQL操作的对象实例
        /// </summary>
        /// <param name="connectionString">链接字符串</param>
        /// <returns>执行者对象实例</returns>
        public abstract Executor CreateExecutor(string connectionString);

        #endregion
    }
}
